package com.example.attendancesystem_1_8;

import android.Manifest;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.core.app.ActivityCompat;

import java.io.File;
import java.io.IOException;

public class Test_Face extends Test_Base implements View.OnClickListener
{
    private static String[] PERMISSIONS_STORAGE = {Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA };
    private Button Tbutton_up;
    private Button Tbutton_compare;
    private Button button_finish;
    private TextView textView_same;
    private ImageView imageView_test;
    private File tempFile = null;   //新建一个 File 文件（用于相机拿数据）
   // private ImageView imageView_test2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test__face);

        Tbutton_up = findViewById(R.id.Tbutton_up);
        button_finish = findViewById(R.id.button_finish);
        Tbutton_compare = findViewById(R.id.Tbutton_compare);
        textView_same = findViewById(R.id.textView_same);
        imageView_test = findViewById(R.id.imageView_test);
       // imageView_test2 = findViewById(R.id.imageView_test2);

        Tbutton_compare.setOnClickListener(this);
        button_finish.setOnClickListener(this);
        Tbutton_up.setOnClickListener(this);


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.Tbutton_up:
                toPicture();
                Toast.makeText(Test_Face.this,"Button 01",Toast.LENGTH_SHORT).show();
                break;
            case R.id.Tbutton_compare:
                //检查是否已经获得相机的权限
                if(verifyPermissions(Test_Face.this,PERMISSIONS_STORAGE[2]) == 0){
                   // L.e("提示是否要授权");
                    ActivityCompat.requestPermissions(Test_Face.this, PERMISSIONS_STORAGE, 3);

                }else{
                    //已经有权限
                    toCamera();  //打开相机
                }
                break;
            case R.id.button_finish:
                textView_same.setText("91.6678%");
                break;
        }
    }

    //获取 相机 或者 图库 返回的图片
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        //判断返回码不等于0
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode != RESULT_CANCELED) {    //RESULT_CANCELED = 0(也可以直接写“if (requestCode != 0 )”)
            //读取返回码
            switch (requestCode) {
                case 100:   //相册返回的数据（相册的返回码）
                    //L.e("相册");
                    Uri uri01 = data.getData();
                    try {
                        Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri01));
                        //ImageView01.setImageBitmap(bitmap);
                        //imageView_test2.setImageBitmap(bitmap);
                        imageView_test.setImageBitmap(bitmap);

                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                    break;
                case 101:  //相机返回的数据（相机的返回码）
                    // L.e("相机");
                    try {
                        tempFile = new File(Environment.getExternalStorageDirectory(), "fileImg.jpg");  //相机取图片数据文件
                        Uri uri02 = Uri.fromFile(tempFile);   //图片文件
                        Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri02));
                        imageView_test.setImageBitmap(bitmap);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    break;
            }
        }
    }

    //跳转相册
    private void toPicture() {
        Intent intent = new Intent(Intent.ACTION_PICK);  //跳转到 ACTION_IMAGE_CAPTURE
        intent.setType("image/*");
        startActivityForResult(intent,100);
        //L.e("跳转相册成功");
    }

    //跳转相机
    private void toCamera() {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);  //跳转到 ACTION_IMAGE_CAPTURE
        //判断内存卡是否可用，可用的话就进行存储
        //putExtra：取值，Uri.fromFile：传一个拍照所得到的文件，fileImg.jpg：文件名
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Environment.getExternalStorageDirectory(),"fileImg.jpg")));
        startActivityForResult(intent,101); // 101: 相机的返回码参数（随便一个值就行，只要不冲突就好）
        //L.e("跳转相机成功");
    }


}